package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.CmsBuyCredits;
import com.ruoyi.system.mapper.CmsBuyCreditsMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ICmsBuyCreditsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 比特币充值凭证Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-15
 */
@Service
public class CmsBuyCreditsServiceImpl implements ICmsBuyCreditsService {
    @Autowired
    private CmsBuyCreditsMapper cmsBuyCreditsMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询比特币充值凭证
     *
     * @param id 比特币充值凭证主键
     * @return 比特币充值凭证
     */
    @Override
    public CmsBuyCredits selectCmsBuyCreditsById(Long id) {
        return cmsBuyCreditsMapper.selectCmsBuyCreditsById(id);
    }

    /**
     * 查询比特币充值凭证列表
     *
     * @param cmsBuyCredits 比特币充值凭证
     * @return 比特币充值凭证
     */
    @Override
    public List<CmsBuyCredits> selectCmsBuyCreditsList(CmsBuyCredits cmsBuyCredits) {
        List<CmsBuyCredits> cbc;
        if (SecurityUtils.isAdmin(getUserId())) {
            cbc = cmsBuyCreditsMapper.selectCmsBuyCreditsList(cmsBuyCredits);
        } else {
            cmsBuyCredits.setAccount(getUserId());
            cbc = cmsBuyCreditsMapper.selectCmsBuyCreditsList(cmsBuyCredits);
        }
        cbc.forEach(c -> {
            SysUser sysUser = sysUserMapper.selectUserById(c.getAccount());
            Optional.ofNullable(sysUser).ifPresent(u -> c.setAccountName(u.getUserName()));
        });
        return cbc;
    }

    /**
     * 新增比特币充值凭证
     *
     * @param cmsBuyCredits 比特币充值凭证
     * @return 结果
     */
    @Override
    public int insertCmsBuyCredits(CmsBuyCredits cmsBuyCredits) {
        Long userId = getUserId();
        cmsBuyCredits.setAccount(userId);
        cmsBuyCredits.setCreateBy(SecurityUtils.getUsername());
        cmsBuyCredits.setCreateTime(new Date());
        return cmsBuyCreditsMapper.insertCmsBuyCredits(cmsBuyCredits);
    }

    /**
     * 修改比特币充值凭证
     *
     * @param cmsBuyCredits 比特币充值凭证
     * @return 结果
     */
    @Override
    public int updateCmsBuyCredits(CmsBuyCredits cmsBuyCredits) {
        cmsBuyCredits.setUpdateBy(SecurityUtils.getUsername());
        return cmsBuyCreditsMapper.updateCmsBuyCredits(cmsBuyCredits);
    }

    /**
     * 批量删除比特币充值凭证
     *
     * @param ids 需要删除的比特币充值凭证主键
     * @return 结果
     */
    @Override
    public int deleteCmsBuyCreditsByIds(Long[] ids) {
        return cmsBuyCreditsMapper.deleteCmsBuyCreditsByIds(ids);
    }

    /**
     * 删除比特币充值凭证信息
     *
     * @param id 比特币充值凭证主键
     * @return 结果
     */
    @Override
    public int deleteCmsBuyCreditsById(Long id) {
        return cmsBuyCreditsMapper.deleteCmsBuyCreditsById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult updateUserIntegral(CmsBuyCredits cc) {
        CmsBuyCredits cbc = new CmsBuyCredits();
        cbc.setId(cc.getId());
        cbc.setStatus(1);
        cmsBuyCreditsMapper.updateCmsBuyCredits(cbc);
        CmsBuyCredits cmsBuyCredits = cmsBuyCreditsMapper.selectCmsBuyCreditsById(cc.getId());
        SysUser su = sysUserMapper.selectUserById(cmsBuyCredits.getAccount());
        SysUser sysUser = new SysUser();
        sysUser.setUserId(cc.getAccount());
        BigDecimal bigDecimal = Optional.ofNullable(su.getIntegral())
                .map(s -> s.add(cmsBuyCredits.getUsd())).orElse(BigDecimal.ZERO);
        sysUser.setIntegral(bigDecimal);
        int count = sysUserMapper.updateUserIntegral(sysUser);
        if (count > 0) {
            return AjaxResult.success();

        } else {
            return AjaxResult.error();
        }
    }
}
